package com.yfl.tingyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yfl.tingyu.pojo.MarriedPerson;
import com.yfl.tingyu.service.impl.MarriedPersonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-05-31
 */
@Controller
@RequestMapping("/married")
public class MarriedPersonController {

    @Autowired
    private MarriedPersonServiceImpl marriedPersonService;

    @RequestMapping("/marriedManager")
    public String carriedPersonManager(){

        return "marriedManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(Integer page, Integer rows, @RequestParam Map<String,Object> params){

        Map<String, Object> marriedPerson = marriedPersonService.selectMarride(page, rows,params);

        return  marriedPerson;
    }

    //修改状态
    @RequestMapping("/changeStatus")
    @ResponseBody
    public Boolean changeStatus(Integer pid,String status){


        String sta="";
        if (status.equals("0")){
            sta="1";
        }
        if (status.equals("1")){
            sta="0";
        }

        MarriedPerson carriedPerson=new MarriedPerson();
        carriedPerson.setPid(pid);
        carriedPerson.setStatus(sta);
        boolean result = marriedPersonService.updateById(carriedPerson);
        return result;
    }


    //添加,修改账号
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Boolean saveOrUpdate(MarriedPerson marriedPerson){

        //设置注册时间为当前时间
        //判断是否是新增操作
        System.out.println("参数"+marriedPerson);
        if (marriedPerson.getPid() == null) {
            marriedPerson.setRegdate(LocalDateTime.now());
        }//
        boolean saveOrUpdate = marriedPersonService.saveOrUpdate(marriedPerson);

        return saveOrUpdate;
    }

    //删除账号
    @RequestMapping("/deleteMarried")
    @ResponseBody
    public Boolean deleteCompnay(Integer[] pids){

        System.out.println("参数："+ Arrays.asList(pids));
        QueryWrapper<MarriedPerson> qw=new QueryWrapper<>();
        qw.in("pid",pids);
        boolean remove = marriedPersonService.remove(qw);
        return remove;
    }


}

